<?php
/**
 * 网站管理
 * Created by PhpStorm.
 * User: qinzhi
 * Date: 2017/8/22
 * Time: 14:17
 */

class Controller_Adminmall_Website extends Controller_Adminmall_Abstract
{
    private $DB_SERVER = "xhs_slave";
    private $DB_SERVER_MASTER = "xhs_master";

    /**
     * 基本配置
     */
    public function configAction(){
        $id = 1;
        global $_F;
        if($this->isPost()){
            $price_send = FRequest::getPostFloat('price_send');
            $first_reduce_money = FRequest::getPostFloat('first_reduce_money');
            $distance = FRequest::getPostFloat('distance') ? FRequest::getPostFloat('distance') : 0;
            $cue_words = FRequest::getPostString('cue_words') ? FRequest::getPostString('cue_words') : '';
            $store_close_words = FRequest::getPostString('store_close_words') ? FRequest::getPostString('store_close_words') : '';
            $btm_cue_words = FRequest::getPostString('btm_cue_words') ? FRequest::getPostString('btm_cue_words') : '';
            if($btm_cue_words) $btm_cue_words = str_replace('"',"'",$btm_cue_words);
            $pay_money = $_POST['pay_money'];
            $reduce_money = $_POST['reduce_money'];
            $full_discount = '';
            if(is_array($pay_money) && count($pay_money)>0){
                foreach ($pay_money as $k=>$val){
                    $full_discount[$k]['pay_money'] = $pay_money[$k];
                    $full_discount[$k]['reduce_money'] = $reduce_money[$k];
                }
                $full_discount = json_encode($full_discount);
            }

            $table = new FTable('peizhi', '',$this->DB_SERVER_MASTER);
            $data = array(
                'price_send' => $price_send,
                'first_reduce_money' => $first_reduce_money,
                'full_discount' => $full_discount,
                'distance' => $distance,
                'cue_words' => $cue_words,
                'store_close_words' => $store_close_words,
                'btm_cue_words' => $btm_cue_words,
            );

            $ret = $table->where(array('id' => $id))->save($data);
            if($ret){
                Service_Order::clearPeizhiCache();
                $this->success('修改成功', $_F['refer']);
            }else{
                $this->error('修改失败', $_F['refer']);
            }
        }

        $table = new FTable('peizhi', '',$this->DB_SERVER);
        $data = $table->where("id={$id}")->find();
        $this->assign('full_discount',json_decode($data['full_discount'],true));
        $this->assign('data', $data);


        $this->display('config');
    }

    public function uploadImgAction(){
        require_once(APP_ROOT . 'lib/weixin/WeixinChat.class.php');
        $options = array(
            'token' => FConfig::get('wechat.token'),
            'appid' => FConfig::get('wechat.appid'),
            'appsecret' => FConfig::get('wechat.appsecret'),
        );
        $weixin = new WeixinChat($options);
        $weixin->checkAuth();
        $post_data = array(
            'type' => 'image',
            'offset' => 0,
            'count' => 20
        );
        $result = $weixin->batchget_material($post_data);
        var_dump($result);
    }
}